package com.bigknow.appstore.web.framework.service;

import com.bigknow.appstore.web.framework.dao.IDao;
import com.bigknow.appstore.web.framework.entity.IEntity;
import com.bigknow.appstore.common.util.StringUtils;
import com.bigknow.appstore.web.framework.vo.Page;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

/**
 * Default service not load reference object.Do it for inherit class.
 */
public class BaseCRUDService<T extends IEntity, E extends IDao<T>> extends BaseService implements
        ICRUDService<T> {

    protected AtomicLong counter = new AtomicLong(System.currentTimeMillis());

    @Autowired
    protected E dao;

    @Override
    public T get(String id) {
        return this.dao.get(id);
    }

    @Override
    public int save(T entity) {
        if (StringUtils.isBlank(entity.getId())) {
            entity.setId(String.valueOf(counter.getAndIncrement()));
        }
        return this.dao.save(entity);
    }

    @Override
    public int insert(T t) {
        t.setId(String.valueOf(counter.getAndIncrement()));
        return this.save(t);
    }

    @Override
    public int update(T t) {
        if (StringUtils.isBlank(t.getId())) {
            return 0;
        } else {
            return this.save(t);
        }
    }

    @Override
    public int delete(String id) {
        return this.dao.delete(id);
    }

    @Override
    public long getCount() {
        return this.dao.getCount();
    }

    @Override
    public List<T> find(T entity) {
        return this.dao.find(entity);
    }

    @Override
    public List<T> find(T t, int limit) {
        return this.dao.find(t, limit);
    }

    @Override
    public T findFirst(T t) {
        return this.dao.findFirst(t);
    }

    @Override
    public Page<T> find(T entity, int pageIndex, int pageSize) {
        return this.dao.find(entity, pageIndex, pageSize);
    }

    @Override
    public List<T> findWithFuzzy(T t) {
        return this.dao.findWithFuzzy(t);
    }

    @Override
    public Page<T> findWithFuzzy(T t, int pageIndex, int pageSize) {
        return this.dao.findWithFuzzy(t, pageIndex, pageSize);
    }

    @Override
    public List<T> findAll() {
        return dao.findAll();
    }

    @Override
    public List<T> findLimit(int limit) {
        return this.dao.findLimit(limit);
    }

    protected String clearUpIds(String idsStr) {
        // Clear up repetition id.
        if (StringUtils.isNotBlank(idsStr)) {
            String[] ids = idsStr.split(",");
            Map<String, String> mapIds = new HashMap<>();
            for (int i = 0; i < ids.length; i++) {
                mapIds.put(ids[i], ids[i]);
            }

            StringBuilder sb = new StringBuilder();
            boolean isFirst = true;
            for (String id : mapIds.keySet()) {
                if (isFirst) {
                    sb.append(id);
                    isFirst = false;
                } else {
                    sb.append(",").append(id);
                }
            }
            return sb.toString();
        }

        return idsStr;

    }
}
